package demo;

/**
 *  最长回文子串
 */
class Solution19 {
	public String longestPalindrome(String s) {
		String res = "";
		for (int i = 0; i < s.length(); i++) {
			String s1 = palindrom(s, i, i);
			String s2 = palindrom(s, i, i + 1);

			res = res.length() > s1.length() ? res : s1;
			res = res.length() > s2.length() ? res : s2;
		}
		return res;
	}

	private String palindrom(String s, int l, int r) {
		while (l <= r && r < s.length() && l >= 0) {
			if (s.charAt(l) == s.charAt(r)) {
				l--;
				r++;
			} else {
				break;
			}
		}
		return s.substring(l + 1, r);
	}
}

